package com.erbao.thread.juc;

import com.erbao.SleepUtil;
import com.erbao.datastructurearithmetic.RandomUtil;

import java.util.concurrent.Phaser;
import java.util.concurrent.TimeUnit;

/**
 * @author CaoBaoYin
 * @since 2022/2/26 17:23
 */
public class PhaserTest {
    // private Phaser phaser = new Phaser(7);
    // private Phaser phaser = new Phaser();
    private static MarriagePhaser phaser = new MarriagePhaser();
    private static int personCount = 7;

    public static void main(String[] args) {
        phaser.bulkRegister(personCount);//几个线程参与，也可以用构造方法传入
        Person p[] = new Person[personCount];

        for (int i = 0; i < personCount-2; i++) {
            p[i] = new Person("guest_"+i, phaser);
        }

        p[personCount-2] = new Person("新郎", phaser);
        p[personCount-1] = new Person("新娘", phaser);

        for (Person person : p) {
            person.start();
        }
    }
}

class Person extends Thread {
    private String name;
    private Phaser phaser;

    public Person(String name, Phaser phaser) {
        this.name = name;
        this.phaser = phaser;
    }

    public void arrive () {
        SleepUtil.sleep(500L);
        System.out.printf("%s 到达现场", this.name);
        System.out.println();
        phaser.arriveAndAwaitAdvance();// 等待进入下一阶段
    }
    public void eat () {
        SleepUtil.sleep(500L);
        System.out.printf("%s 吃饭", this.name);
        System.out.println();
        phaser.arriveAndAwaitAdvance();
    }
    public void leave () {
        SleepUtil.sleep(500L);
        System.out.printf("%s 离开", this.name);
        System.out.println();
        phaser.arriveAndAwaitAdvance();
    }
    public void hug () {
        if ("新郎".equals(this.name) || "新娘".equals(this.name)) {
            SleepUtil.sleep(500L);
            System.out.printf("%s 准备洞房", this.name);
            System.out.println();
            phaser.arriveAndAwaitAdvance();//完成，等待进入下一阶段
        } else {
            phaser.arriveAndDeregister();// 完成，不再进入下一阶段
        }
    }

    @Override
    public void run() {
        arrive();

        eat();

        leave();

        hug();
    }
}

class MarriagePhaser extends Phaser {
    @Override
    protected boolean onAdvance(int phase, int registeredParties) {
        switch (phase) {
            case 0 :{
                System.out.println(new StringBuilder("所有人到齐了").append("_").append(phase).append("_").append(registeredParties));
                System.out.println();
                return false;
            }
            case 1 :{
                System.out.println(new StringBuilder("所有人吃完了").append("_").append(phase).append("_").append(registeredParties));
                System.out.println();
                return false;
            }
            case 2 :{
                System.out.println(new StringBuilder("宾客离开了").append("_").append(phase).append("_").append(registeredParties));
                System.out.println();
                return false;
            }
            case 3 :{
                System.out.println(new StringBuilder("新郎新娘开始洞房").append("_").append(phase).append("_").append(registeredParties));
                System.out.println();
                return true;
            }
            default: {
                return true;
            }
        }
    }
}
